package org.agilelang.plugin.manager;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.agilelang.plugin.entities.ListPlugin;
import org.agilelang.plugin.entities.Plugin;

public class ManagerPlugin {

    public ListPlugin listPlugins = new ListPlugin();
    Plugin plugin = new Plugin();
    public String path_xml = "";

    public void read() {
        File file = new File(path_xml);
        if (!file.exists()) {
            System.err.println("Fichero XML no existe");
            return;
        }
        //leer xml
        Reader reader = null;
        try {
            InputStream is = new FileInputStream(path_xml);
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            System.out.println("Reding xml...");
        } catch (Exception ex) {
            System.err.println("Error al leer fichero XML : " + ex);
        }
        //Poblar objecto Project  
        try {
            JAXBContext jc = JAXBContext.newInstance(ListPlugin.class);
            Unmarshaller u = jc.createUnmarshaller();
            listPlugins = (ListPlugin) u.unmarshal(reader);
            System.out.println("Built project...");
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }

    }

    public Plugin readOne(String path_xml) {
        File file = new File(path_xml);
        if (!file.exists()) {
            System.err.println("Fichero XML no existe");
            return null;
        }
        //leer xml
        Reader reader = null;
        try {
            InputStream is = new FileInputStream(path_xml);
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            System.out.println("Reding xml...");
        } catch (Exception ex) {
            System.err.println("Error al leer fichero XML : " + ex);
        }
        //Poblar objecto Project  
        try {
            JAXBContext jc = JAXBContext.newInstance(Plugin.class);
            Unmarshaller u = jc.createUnmarshaller();
            plugin = (Plugin) u.unmarshal(reader);
            System.out.println("Built project...");
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }
        return plugin;
    }

    public void write() {
        Marshaller marshaller;
        java.io.StringWriter sw = new StringWriter();
        try {
            JAXBContext jc = JAXBContext.newInstance(ListPlugin.class);
            marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(listPlugins, sw);

        } catch (JAXBException ex) {
            System.out.println(ex.toString());
        }

        try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(path_xml)))) {
            pw.print(sw.toString());
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
    }

    public void add(Plugin plugin) {
        listPlugins.addPlugin(plugin);
    }

    public void remove(String title) {
        List<Plugin> tmp_plugins = new ArrayList<>();
        for (Plugin tmp_plugin : listPlugins.plugins) {
            if (!tmp_plugin.title.equals(title)) {
                tmp_plugins.add(tmp_plugin);
            }
        }
        listPlugins.plugins = tmp_plugins;
    }

    public Plugin find(String title) {
        for (Plugin tmp_plugin : listPlugins.plugins) {
            if (tmp_plugin.title.equals(title)) {
                return tmp_plugin;
            }
        }
        return null;
    }

    public String findStr(String title) {
        String str_plugin = "";
        for (Plugin tmp_plugin : listPlugins.plugins) {
            if (tmp_plugin.title.equals(title)) {
                str_plugin += "\n    " + tmp_plugin.title + "\n\n";
                str_plugin += "Category : " + tmp_plugin.category + "\n";
                str_plugin += "Version : " + tmp_plugin.version + "\n";
                str_plugin += "Source : " + tmp_plugin.source + "\n";
                str_plugin += "Jar : " + tmp_plugin.jar + "\n";
                str_plugin += "Home Page : " + tmp_plugin.page + "\n";
                str_plugin += "Description : " + tmp_plugin.description + "\n";
                return str_plugin;
            }
        }
        return str_plugin;
    }
}
